﻿using ArcGIS.Core.Data;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using Aspose.Cells;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CCTool.Scripts.Attribute.FieldFloat
{
    /// <summary>
    /// Interaction logic for CreateRangeNumber.xaml
    /// </summary>
    public partial class CreateRangeNumber : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 工具设置标签
        readonly string toolSet = "CreateRangeNumber";

        public CreateRangeNumber()
        {
            InitializeComponent();

            // 初始化其它参数选项
            txt_min.Text = BaseTool.ReadValueFromReg(toolSet, "min", "0");
            txt_max.Text = BaseTool.ReadValueFromReg(toolSet, "max", "1");
            txt_decimal.Text = BaseTool.ReadValueFromReg(toolSet, "dec", "2");

            rb_int.IsChecked = BaseTool.ReadValueFromReg(toolSet, "isInt", "true").ToBool();
            rb_float.IsChecked = BaseTool.ReadValueFromReg(toolSet, "isFloat", "false").ToBool();

        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "生成随机数";

        private void combox_fc_DropDown(object sender, EventArgs e)
        {
            UITool.AddFeatureLayerAndTableToComboxPlus(combox_fc);
        }

        private void combox_field_DropDown(object sender, EventArgs e)
        {
            UITool.AddMathFieldsToComboxPlus(combox_fc.ComboxText(), combox_field);
        }

        private async void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 获取指标
                string fc = combox_fc.ComboxText();
                string field = combox_field.ComboxText();
                double min = txt_min.Text.ToDouble();
                double max = txt_max.Text.ToDouble();
                int dec = txt_decimal.Text.ToInt();

                bool isInt =(bool)rb_int.IsChecked;
                bool isFloat = (bool)rb_float.IsChecked;

                // 判断参数是否选择完全
                if (fc == "" || field == "")
                {
                    MessageBox.Show("有必选参数为空。");
                    return;
                }

                if (max <= min)
                {
                    MessageBox.Show("最大值小于或等于最小值。");
                    return;
                }

                if (isFloat && dec <= 0)
                {
                    MessageBox.Show("请设定正确的小数位数。");
                    return;
                }
                var type = await QueuedTask.Run(() =>
                {
                    return GisTool.GetFieldType(fc, field);
                });
                // 小数型，字段却是整数型
                if (isFloat && (type == FieldType.Integer || type == FieldType.SmallInteger || type == FieldType.BigInteger))
                {
                    MessageBox.Show("输出类型为小数型，请选择一个浮点型字段。");
                    return;
                }

                // 写入本地
                BaseTool.WriteValueToReg(toolSet, "min", min);
                BaseTool.WriteValueToReg(toolSet, "max", max);
                BaseTool.WriteValueToReg(toolSet, "dec", dec);
                BaseTool.WriteValueToReg(toolSet, "isInt", isInt);
                BaseTool.WriteValueToReg(toolSet, "isFloat", isFloat);

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                Close();

                await QueuedTask.Run(() =>
                {
                    //  整数型
                    if (isInt)
                    {
                        pw.AddMessageStart($"生成整数型随机数，{min}~{max}");
                        string block = $"import random\r\ndef ss():\r\n    random_float = random.randint({min},{max})\r\n    return random_float";
                        Arcpy.CalculateField(fc, field, $"ss()", block);
                    }
                    // 小数型
                    else if (isFloat)
                    {
                        pw.AddMessageStart($"生成整数型随机数，小数位数{dec}，{min}~{max}");
                        string block = $"import random\r\ndef ss():\r\n    random_float = random.uniform({min},{max})\r\n    return round(random_float,{dec})";
                        Arcpy.CalculateField(fc, field, $"ss()", block);
                    }
                });

                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void btn_help_Click(object sender, RoutedEventArgs e)
        {
            string url = "https://blog.csdn.net/xcc34452366/article/details/152331635";
            UITool.Link2Web(url);
        }
    }
}
